package com.wujilee.orderservice;

import com.wechat.pay.java.service.payments.model.Transaction;
import com.wechat.pay.java.service.payments.nativepay.NativePayService;
import com.wechat.pay.java.service.payments.nativepay.model.QueryOrderByOutTradeNoRequest;
import com.wujilee.dbentity.entity.EntityOrderInfo;
import com.wujilee.orderservice.service.OrderService;
import com.wujilee.payservice.config.WeixinPayConfig;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Iterator;

/**
 * @author liwuji
 * @date 2024/6/18
 * @desc
 **/
@Service
@EnableScheduling
public class NotifyScheduled {
    @Resource
    private NativePayService nativePayService;
    @Resource
    private WeixinPayConfig weixinPayConfig;
    @Resource
    private OrderService orderService;
    /**
     * 定时查找超时订单
     * 从第0秒开始每隔30秒执行1次，查询创建超过5分钟，并且未支付的订单
     */
    
    //@Scheduled(cron = "0/30 * * * * ?")
    public void handleScheduled() {

        // 获取超时未收到通知的订单 获取规则 
        
        Iterator<EntityOrderInfo> orderInfos = null;
        while (orderInfos.hasNext()) {
            EntityOrderInfo next = orderInfos.next();
            QueryOrderByOutTradeNoRequest queryOrderByOutTradeNoRequest = new QueryOrderByOutTradeNoRequest();
            queryOrderByOutTradeNoRequest.setOutTradeNo(next.getOrderNo());
            queryOrderByOutTradeNoRequest.setMchid(weixinPayConfig.getMerchantId());
            queryOrderByOutTradeNoRequest.setOutTradeNo(next.getOrderNo());
            Transaction transaction = nativePayService.queryOrderByOutTradeNo(queryOrderByOutTradeNoRequest);
            orderService.notifyOrder(transaction);
        }
    }
}
